#import "SMRSubtleScrollView.h"
#import "SMRSubtleResizeControl.h"
#import "SMRSubtleScroller.h"
#import "NSBezierPath+SMRAdditions.h"
#import "NSShadow+MCAdditions.h"


#pragma mark Constants
static const NSUInteger SMRSubtleScrollViewResizerTag = 1111;


#pragma mark -
@interface SMRSubtleScrollView (SMRSubtleScrollViewPrivateMethods)
-(void)subtleScrollViewCommonInit_;
@end


#pragma mark -
@implementation SMRSubtleScrollView

@synthesize delegate;
@synthesize resizeControl;
@synthesize selected;


// FIXME: THIS IS A PRIVATE METHOD.  HOWEVER IF YOU DON'T OVERRIDE THIS, YOU GET ERRATIC BEHAVIOR WHEN A TEXT VIEW IS THE DOCUMENT VIEW BECAUSE WE'RE CHANGING THE WIDTH OF THE SCROLLER AND SCROLLVIEW POLLS 'NSSCROLLER' RATHER THAN THE CLASS OF THE ACTUAL SCROLLER OBJECT.  THIS PRIVATE METHOD FORCES THE SCROLL VIEW TO POLL THE CORRECT CLASS FOR THE CORRECT SIZE
+(Class)_verticalScrollerClass {
	return [SMRSubtleScroller class];
}


#pragma mark -
#pragma mark Initialization

-(id)initWithFrame:(NSRect)frameRect {
	if (!(self = [super initWithFrame:frameRect]))
		return self;
	[self subtleScrollViewCommonInit_];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return self;
	[self subtleScrollViewCommonInit_];
	return self;
}

-(void)subtleScrollViewCommonInit_ {
	NSView *existingResizer = [self viewWithTag:SMRSubtleScrollViewResizerTag];
	if (existingResizer && [existingResizer isKindOfClass:[SMRSubtleResizeControl class]]) 
		[existingResizer removeFromSuperview];
	SMRSubtleResizeControl *resizer = [[[SMRSubtleResizeControl alloc] initWithFrame:NSZeroRect] autorelease];
	[self setResizeControl:resizer];
	[resizer setParentScrollView:self];
	[resizer setTag:SMRSubtleScrollViewResizerTag];
	[self addSubview:resizer];
}


#pragma mark -
#pragma mark Drawing

-(void)drawRect:(NSRect)dirtyRect {
}


#pragma mark -
#pragma mark Layout

-(void)resizeWithRect:(NSRect)resizeRect {
	[self setFrame:resizeRect];
}

-(void)tile {
	[super tile];
	NSRect scrollViewRect = [self bounds];
	CGFloat scrollerWidth = [[[self verticalScroller] class] scrollerWidth];
	CGFloat scrollerMinX = NSMaxX(scrollViewRect) - scrollerWidth;
	CGFloat scrollerMinY = NSMinY(scrollViewRect);
	CGFloat scrollerHeight = NSHeight(scrollViewRect) - scrollerWidth;
	NSRect scrollerRect = NSMakeRect(scrollerMinX, scrollerMinY, scrollerWidth, scrollerHeight);
	[[self verticalScroller] setFrame:scrollerRect];
	
	NSRect resizeRect = NSMakeRect(scrollerMinX, scrollerHeight, scrollerWidth, scrollerWidth);
	[[self resizeControl] setFrame:resizeRect];
	if ([[self delegate] respondsToSelector:@selector(shouldHideResizeViewForScrollView:)])
		[[self resizeControl] setHidden:[[self delegate] shouldHideResizeViewForScrollView:self]];

	NSRect contentRect = [[self contentView] frame];
	contentRect.size.width = NSWidth(scrollViewRect) - scrollerWidth;
	[[self contentView] setFrame:contentRect];
}

-(void)setSelected:(BOOL)shouldSelect {
	selected = shouldSelect;
	[(SMRSubtleScroller *)[self verticalScroller] setSelected:shouldSelect];
	[[self resizeControl] setSelected:shouldSelect];
}

@end
